import lombok.Getter;
import lombok.Setter;
import utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/message/add")
public class AddMessageServlet  extends HttpServlet {
    public static List<Message> msgList = new ArrayList<>();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int result = -1;
        //1.得到前端参数并效验
        String from = req.getParameter("from");
        String to = req.getParameter("to");
        String msg = req.getParameter("msg");
        if (StringUtils.hasLength(from) && StringUtils.hasLength(to) && StringUtils.hasLength(msg)) {
            //2.将表白对象加入到集合(内存)
            AddMessageServlet.msgList.add(new Message(from, to, msg));
            result = 1;
            //
        }
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().println(result);

        }
    }

@Setter
@Getter
class Message{
    private String from;
    private String to;
    private String message;

    public Message(String from, String to, String message) {
        this.from = from;
        this.to = to;
        this.message = message;
    }
}
